2016-05-05
2016-05-05
Some additional knowledge and handy packages for using R as your data manipulation and web scraping tool
Libraries in R are loaded in order:
.libPaths()## [1] "/usr/local/lib/R/site-library" "/usr/lib/R/site-library"
## [3] "/usr/lib/R/library"
You can also add an additional library path:
.libPaths(c("your/library/path", .libPaths()))Sometime you may need to execute your R script periodically and automatically:
Environment variable of R and RScript will be exported in this way.
Rscript <PATH OF YOUR R SCRIPT> <ARG1> <ARG2> ...If you use Linux or Mac, you can execute a R script directly with shebang and permission of execution.
#!/usr/bin/env RscriptAdd permission of execution:
chmod u+x schedualing/now.RCheck permission:
ls -l schedualing/now.R## -rwxrw-rw- 1 mansun mansun 1130 May 5 16:19 schedualing/now.R
In most situation, I will use a package to dealing with arguments parsing.
#!/usr/bin/env Rscript
# Use argparser
library(argparser, quietly = TRUE)
# Create an arg.parser object.
p <- arg_parser("Hi! What time it is?")
# Add a positional argument
p <- add_argument(p, arg = "who", help = "Who are you")
# Add an optional argument
# Rscript will raise a warning message if you pass with -g
# but it doenn't matter
p <- add_argument(p, arg = "--greeting", short = "-g", default = "How's going?", type = "character",
help = "Greeting word")
# Add a flag, default value is FALSE
p <- add_argument(p, arg = "--chat", short = "-c", flag = TRUE,
help = "Whether or not to have a greeting")
# Parse commandArgs(trailingOnly = TRUE) into args
args <- parse_args(p)
print(args)
str(args)
# The original arguments
command_args <- commandArgs(trailingOnly = TRUE)
cat("args:")
print(command_args)
# Get system time
now <- as.character(Sys.time())
# Construct greeting string
greeting <- sprintf("Hi %s! It is %s.", args$who, now)
if (args$chat) {
greeting <- paste(greeting, args$greeting)
}
print(greeting)
# Write to a text file
writeChar(greeting, "now.txt")Let’s try to pass some arguments to schedualing/now.R:
Get help:
schedualing/now.R -hPass a positional argument:
schedualing/now.R MansunAdd a flag -c:
schedualing/now.R Mansun -cAdd a optional argument:
schedualing/now.R Mansun -c -g "How are you?"Maintain crontab files to execute scheduled commands in Unix-like OS for individual users.
crontab [-u user] file
crontab [ -u user ] [ -i ] { -e | -l | -r }
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
You may need to use some command line editor like vim when using crontab -e to edit your crontab. Type select-editor in terminal to choose your favorite editor
# Execute every minutes
* * * * * cd /home/mansun/github/BeyondBasicR/schedualing; ./now.R Mansuncrontab schedualing/crontab.txt
crontab -l## # Execute every minutes
## * * * * * cd /home/mansun/github/BeyondBasicR/schedualing; ./now.R Mansun
┌───────────── min (0 - 59)
│ ┌────────────── hour (0 - 23)
│ │ ┌─────────────── day of month (1 - 31)
│ │ │ ┌──────────────── month (1 - 12)
│ │ │ │ ┌───────────────── day of week (0 - 6) (0 to 6 are Sunday to Saturday)
│ │ │ │ │
* * * * * command to execute
# Execute at 00:00 and 12:00 everyday
0 0,12 * * * command to execute
# Execute at 06:00 every Monday to Friday
0 6 * * 1-5 command to execute
Please refer to Cron For further information.
Launch task scheduler on Windows:
Launch task schedualer within RStudio:
# Execute a system conmmand to launch task schedualer
system("control schedtasks")Set a schedualing job on your environment